package com.scs.application.modules.adm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.adm.entity.MatrApply;
import com.scs.application.modules.adm.entity.MatrApplyItem;
import com.scs.application.modules.adm.service.MatrApplyItemService;
import com.scs.application.modules.adm.service.MatrApplyService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 耗材准入申请单 前端控制器
 */
@RestController
@RequestMapping("/adm/matr_apply")
public class MatrApplyController extends FlowController<MatrApplyService, MatrApply>{

    @Autowired
    MatrApplyItemService matrApplyItemService;

    /**
     * @Description： 耗材准入申请单视图_选择 准入审批使用
     */
    @GetMapping("/pageForAuditSelect")
    public RestResult pageForAuditSelect(){
        QueryContext queryContext = QueryContextHolder.currentContext();
        List<MatrApplyItem> matrApplyItems = matrApplyItemService.list(Wrappers.<MatrApplyItem>query().select("apply_id").isNull("audit_item_id"));

        if(matrApplyItems != null && matrApplyItems.size() >0){
            queryContext.getWrapper().in("id", matrApplyItems.stream().distinct().map(MatrApplyItem::getApplyId).collect(Collectors.toList()));
        } else {
            queryContext.getWrapper().eq("id", "-1");
        }

        Page<?> pageResult = service.page(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    @GetMapping("/copy/{id}")
    public RestResult copy(@PathVariable String id) {
        return RestResult.ok("复制成功",service.copy(id));
    }
}
